import json
import requests
from tools import config
from tools.RsaClass import rsaclass


class Unit:
    def __init__(self):
        self.headers = config.headers
        self.domain = config.sDomain
        self.product = config.sProduct
        self.time = config.sTime

    def createUnit(self, unit_name):
        '''
        创建部门
        :param unit_name: 新建部门名称
        '''
        src = "domain=" + self.domain + "&product=" + self.product + "&time=" + self.time + "&unit_name=" + unit_name
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "unit_name": unit_name,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/createUnit", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def deleteUnit(self, unit_id):
        '''
        删除部门
        :param unit_id: 待删除部门ID
        '''
        src = "domain=" + self.domain + "&product=" + self.product + "&time=" + self.time + "&unit_id=" + unit_id
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "unit_id": unit_id,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/deleteUnit", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def getAccountList_account(self, account_name):
        '''
        根据账号名获取账号信息
        '''
        src = "account_name=" + account_name + "&domain=" + self.domain + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "account_name": account_name,
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/getAccountList", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def getAccountList_jobno(self, job_no):
        '''
        根据工号获取账号信息
        '''
        src = "domain=" + self.domain + "&job_no=" + job_no + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "job_no": job_no,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/getAccountList", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def getUnit(self, unit_id):
        '''
        获取部门信息
        :param unit_id: 部门ID
        '''
        src = "domain=" + self.domain + "&product=" + self.product + "&time=" + self.time + "&unit_id=" + unit_id
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "unit_id": unit_id,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/getUnit", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def getUnitList(self):
        '''
        获取所有部门信息
        '''
        src = "domain=" + self.domain + "&product=" + self.product + "&time=" + self.time
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/getUnitList", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

    def getSubUnitList(self, unit_id, recursion="true"):
        '''
        获取子部门列表
        :param unit_id: 一级部门ID
        :param recursion: 值为true时递归取子部门的子部门，false不取
        '''
        src = "domain=" + self.domain + "&product=" + self.product + "&recursion=" + recursion + "&time=" + self.time + "&unit_id=" + unit_id
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "product": self.product,
            "recursion": recursion,
            "time": self.time,
            "unit_id": unit_id,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/getSubUnitList", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())


    def updateUnit(self, unit_id, unit_name, unit_desc=''):
        '''
        更新部门信息
        :param unit_id: 部门ID
        :param unit_name: 修改部门名称
        :param unit_desc: 修改部门描述
        :return:
        '''
        src = "domain=" + self.domain + "&product=" + self.product + "&time=" + self.time + "&unit_desc=" + unit_desc + "&unit_id=" + unit_id + "&unit_name=" + unit_name
        # 数字签名
        sSign = rsaclass(src)
        # 封装参数
        param = {
            "domain": self.domain,
            "product": self.product,
            "time": self.time,
            "unit_desc": unit_desc,
            "unit_id": unit_id,
            "unit_name": unit_name,
            "sign": sSign,
        }
        # 拼接URL
        resp = requests.post(url="https://apibj.qiye.163.com/qiyeservice/api/unit/updateUnit", params=param,
                             headers=self.headers)
        # 使用json.loads将json字符串转成python字典对象
        return json.loads(resp.content.decode())

if __name__ == '__main__':
    ops_obj = Unit()
    # 新建部门
    # print(ops_obj.createUnit('邮箱自动化新建部门'))
    # 删除部门,参数为部门ID
    # ops_obj.deleteUnit('1825913')
    # 根据账号名获取账号信息
    print(ops_obj.getAccountList_account('bing.li03'))
    # 根据工号获取账号信息
    # print(ops_obj.getAccountList_jobno('20849'))
    # 获取部门信息
    # print(ops_obj.getUnit('1825913'))
    # 获取所有部门信息
    # print(ops_obj.getUnitList()['con'])
    # 更新部门信息
    # print(ops_obj.updateUnit('1825913', '自动化新建部门', '自动化修改部门描述'))
    # 获取子部门列表
    # print(ops_obj.getSubUnitList('1818948'))

    # 通过人事组织机构获取邮箱组织架构
    # from UnitSub import getPriUnitName, getUnitName
    #
    # sub_dept_list = ops_obj.getSubUnitList(getPriUnitName('智能物流事业部'))['con']
    # print(sub_dept_list)